package com.suxin.gateway.controller;

import org.springframework.cloud.gateway.support.ServerWebExchangeUtils;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.util.HashMap;
import java.util.Map;

/**
 * @Classname FallbackController
 * @Description [ TODO ]
 * @Author Tang
 * @Date 2020/10/2 23:05
 * @Created by ASUS
 */
@RestController
@RequestMapping("/fallback")
public class FallbackController {

    /**
     * 默认的错误回调处理方法
     * @return
     */
    @GetMapping("/defaultFallback")
    public Mono<Map<String, Object>> fallbackMethod(ServerWebExchange serverWebExchange) {
        Map<String, Object> map = new HashMap<>();
        map.put("code", 400);
        map.put("message", "internal server error, please try later..."
                + ServerWebExchangeUtils.HYSTRIX_EXECUTION_EXCEPTION_ATTR);
        ServerHttpRequest httpRequest = serverWebExchange.getRequest()
                .mutate()
                .header("javax.servlet.error.status_code", "400")
                .build();
        serverWebExchange = serverWebExchange
                .mutate()
                .request(httpRequest)
                .build();
        return Mono.just(map);
    }

    @GetMapping("/index")
    public String index() {
        int i = 1 / 0;
        return "index...";
    }
}